package com.haoziqi.chapter_05;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * description
 * created by A on 2021/3/5
 */
public class ProcessTrans {
    public static void main(String[] args) {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> ds = env.socketTextStream("hadoop102", 9999);
        //ds.map(word -> Tuple2.of("zhao",word)).print();
        SingleOutputStreamOperator<Object> sos = ds.map(new MapFunction<String, Object>() {
            @Override
            public Object map(String value) throws Exception {
                return Tuple2.of("zhao", value);
            }
        });
        sos.process(new ProcessFunction<Object, Object>() {
            @Override
            public void processElement(Object value, Context ctx, Collector<Object> out) throws Exception {
                Long timestamp = ctx.timestamp();
                out.collect(value.toString()+timestamp);

            }
        }).print();

        //提交任务
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
